package com.class2.java.com.class2.java.net;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author: yc
 * @email: 1247616535@qq.com
 * @desc: 该类用于封装提供网络请求的功能代码，用于发起网络请求，并将网络的数据进行返回
 * @datetime: 2021/12/21 17:14
 */
public class HttpUtils {

    /**
     * 该函数用于实现get请求，并将请求的结果进行返回
     *
     * @param path 请求路径
     * @return 请求结果
     */
    public static String get(String path) {
        BufferedReader reader = null;
        InputStreamReader inputStreamReader = null;
        InputStream inputStream = null;


        String line = "";//最开始的空行
        String result = "";//每次读取到以后都有累计起来的读取结果
        try {
            //Union Resource Location 统一资源定位
            URL url = new URL(path);
            URLConnection conn = url.openConnection();
            //进行连接，connect是执行连接的代码
            conn.connect();
            //getInputStream
            inputStream = conn.getInputStream();

            //字符流，按行读
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
//            String line = reader.readLine();
//            while(line !=null){
//                reader.readLine();
//            }
            while ((line = reader.readLine()) != null) {
                result += line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭读取流
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * java原生代码实现post请求，并返回请求结果
     *
     * @param path   请求的路径
     * @param header 请求头信息
     * @param data   请求数据
     * @return 请求的结果
     */

    public static String post(String path, Map<String, String> header, byte[] data) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader reader = null;
        String result = "";
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //请求头设置
            //ContentType:application/json
            //Authorization:"Basic" + "xxxx"
            //如何拿到所有map中的数据
            //keySet()可以获取所有的key，在增强for循环遍历
            if (!header.isEmpty()) {
                Set<String> keys = header.keySet();
                for (String key : keys) {
                    String value = header.get(key);
                    conn.setRequestProperty(key, value);
                }
            }
            conn.connect();//连接

            //将请求数据发送出去
            outputStream = conn.getOutputStream();
            outputStream.write(data);

            //读取响应的数据
            inputStream = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = reader.readLine()) != null) {
                result += line;
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                String a = "没有任何意义";
                if (inputStream != null) {
                    inputStream.close();
                }
                if (reader != null){
                    reader.close();
                }
                if (outputStream != null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "";
    }
}

